<?php

namespace app\index\controller;

use think\facade\Hook;
use think\facade\App;
use think\Container;
use app\common\service\InvokerTest;
use app\common\model\Message;
use think\Db;
use think\Build;
use bqs\validate\Check;
use think\Facade\Log;

class Index
{
    public function index()
    {
        return '<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px;} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:) </h1><p> ThinkPHP V5.1<br/><span style="font-size:30px">12载初心不改（2006-2018） - 你值得信赖的PHP框架</span></p></div><script type="text/javascript" src="https://tajs.qq.com/stats?sId=64890268" charset="UTF-8"></script><script type="text/javascript" src="https://e.topthink.com/Public/static/client.js"></script><think id="eab4b9f840753f8e7"></think>';
    }

    public function test2()
    {
        // Log::write()打印出来的没有样式
        Log::error('yichang data2021');
        Log::info('info test2009');
        Log::notice('notice test2009');
        return "hello test1222";
    }

    public function vendorTest()
    {
        $bqsValidate = new Check();
        return $bqsValidate->isNumber();
    }

    public function modelTest()
    {
        $arr = [
            // 生成应用公共文件
            '__file__' => ['common.php'],

            // 定义demo模块的自动生成 （按照实际定义的文件名生成）
            'demo'     => [
                '__file__'   => ['common.php'],
                '__dir__'    => ['behavior', 'controller', 'model', 'view'],
                'controller' => ['Index', 'GameCenter', 'UserType'],
                'model'      => ['GameCenter', 'UserType'],
                'view'       => ['index/index'],
            ],
            // 其他更多的模块定义
        ];
        $app = Container::get("app");
        $build = new Build($app);

        // 生成系统文件
        //return $build->run($arr, 'app');

        // 生成模块
        //return $build->module('game');

        // 根据控制器注解自动生成路由规则
        return $build->buildRoute('php','application/index/controller');


        $module = '';
        if ($module) {
            return 123456;
        }

        return 231231324;
//        $list = Db::name("message")->select();
//        var_dump($list);die;
//
//        $name       = str_replace('\\', '/', static::class);
//        $basenames = basename($name);
//        return $basenames;

//        $messageObj = new Message();
//        var_dump($messageObj->getGlobalScope());die;

        $data = Message::where('id', 1)->find();
        //return $data->getPk();
        return $data;
    }

    public function modelBaseTest($arr)
    {
        $arr['name'] = 'jiangsu';
        return $arr;
    }


    public function test()
    {

        $res = \think\facade\Route::get('hello/:name', 'index/hello');
        var_dump($res);die;

        //$this->middleware->dispatch($this->request);
        $res = Container::get('middleware')->dispatch($this->request);
        var_dump($res);die;


        $res = Container::get('route')->check("index/Index/test", false);
        var_dump($res);die;


//        $res = Container::get('request')->ext();
//        var_dump($res);die;

        //$this->request->path();
        $res = Container::get('request')->path();
        var_dump($res);die;


        $res = Container::get('route')->checkDomain();
        var_dump($res);die;

        // return App::isDebug();

        // $obj = Container::getInstance()->invokeClass(InvokerTest::class);
        // var_dump($obj->invokerNews());die;

        // $methods = Container::getInstance()->invokeMethod(["app\common\service\InvokerTest", "invokerNews"]);
        // var_dump($methods);die;

        // $functions = Container::getInstance()->invokeFunction(function ($name) {
        //     return "hello {$name}";
        // }, ["beiqiaosu"]);
        // var_dump($functions);die;
        //
        // $functions = Container::getInstance()->invokeFunction("procmsg", ["12312", "heillo"]);
        // var_dump($functions);die;

        $invoke = Container::getInstance()->invoke(["app\common\service\InvokerTest", "invokerNews"]);
        var_dump($invoke);die;


        return "are you ok!!!";
    }

    public static function invokerMethodTest()
    {
        return "z z z";
    }

    public function hello($name = 'ThinkPHP5')
    {
        return 'hello,' . $name;
    }

    public function testHook()
    {
        //$res = [
        //    '0' => [
        //        'a' => 'you',
        //        'b' => 'me'
        //    ],
        //    '1' => [
        //        'c' => 'li',
        //        'd' => 'op'
        //    ]
        //];
        //
        //$res = array_reduce($res,function($result,$value) {
        //
        //    return array_merge($result,array_values($value));
        //
        //},array());

        //function cube($n)
        //{
        //    return($n * $n * $n);
        //}

        //$a = array(1, 2, 3, 4, 5);
        //
        //$res = array_map(function ($value) {
        //    return $value * $value;
        //}, $a);
        ////print_r($b);


        //var_dump(8 & 2); die;

        //$array1 = array("a"=>1, "b"=>2, "c"=>3, "d"=>4, "e"=>5);
        //
        //$array2 = array(6, 7, 8, 9, 10, 11, 12);
        //
        //echo "Odd 单数:\n";
        //
        //print_r(array_filter($array1, function ($var) {
        //
        //    return($var & 1);
        //
        //}));    //1，3，5
        //
        //echo PHP_EOL.PHP_EOL.'*********************分隔线**************************'.PHP_EOL.PHP_EOL;
        //
        //echo "Even双数:\n";
        //print_r(array_filter($array2, function ($var) {
        //    return(!($var & 1));
        //}));//2，4，6
        //
        //die;

        //$testArr = [
        //
        //    //"jiandefe" => [
        //    //    "advert" => [
        //    //        "name" => "测试广告名",
        //    //        "url" => "www.test.com"
        //    //    ],
        //    //],
        //    //"fefeege" => [
        //    //    "pictrue" => [
        //    //        "name" => "测试广告名",
        //    //        "url" => "www.test.com"
        //    //    ],
        //    //]
        //
        //    "1" => [26,22,56,11],
        //    "2" => [15,45,56,89,77]
        //];

        //$testArr = [14,26,11,26,15,16,19];
        //
        //$res = array_filter($testArr, function ($var) {
        //    return ($var&1);
        //});
        //
        //return json($res); die;

        Hook::listen('create_order', ['name' => 'fc']);
    }

}
